import { createRouter, createWebHistory } from 'vue-router'
import loginVue from '../views/loginApp.vue'
import signUpVue from '../views/signUpApp.vue'
import resetPassword from '../views/resetPassword.vue'
import mainTravelerVue from '../views/mainTraveler.vue'
import mainHotelManagerVue from '../views/mainHotelManager.vue'
import mainManagerVue from '../views/mainManager.vue'
import submitComplaintsVue from '../views/travelerView/complaintsView/submitComplaintsView.vue'
import complaintResultVue from '../views/travelerView/complaintsView/complaintsResultView.vue'
import travelermainVue from '../views/travelerView/mainView/traveler-mainView.vue'
import emergencyInfoQueryVue from '../views/travelerView/emergencyInfoView/emergencyInfoQueryView.vue'
import routeViewVue from '../views/travelerView/statusView/routeView.vue'
import weatherViewVue from '../views/travelerView/statusView/weatherView.vue'
import roadstatusViewVue from '../views/travelerView/statusView/roadstatusView.vue'
import scenicQueryVue from '../views/travelerView/travelQueryView/scenicQueryView.vue'
import diningQueryVue from '../views/travelerView/entertainmentView/diningQueryView.vue'
import showQueryVue from '../views/travelerView/entertainmentView/showQueryView.vue'
import hotelOrderVue from '../views/travelerView/hotelOrderView/hotelOrderView.vue'
import { ElMessage } from 'element-plus'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'default',
      redirect: '/login'
    },
    {
      path: '/traveler',
      name: 'traveler',
      component: mainTravelerVue,
      redirect: '/traveler/travelermain',
      children: [
        {
          path: 'complaintsSubmit',
          name: 'complaintsSubmit',
          component: submitComplaintsVue
        },
        {
          path: 'complaintsResult',
          name: 'complaintsResult',
          component: complaintResultVue
        },
        {
          path: 'weather',
          name: 'weather',
          component: weatherViewVue
        },
        {
          path: 'route',
          name: 'route',
          component: routeViewVue
        },
        {
          path: 'status',
          name: 'status',
          component: roadstatusViewVue
        },
        {
          path: 'travelermain',
          name: 'travelermain',
          component: travelermainVue
        },
        {
          path: 'emergencyInfoQuery',
          name: 'emergencyInfoQuery',
          component: emergencyInfoQueryVue
        },
        {
          path: 'scenicQuery',
          name: 'scenicQuery',
          component: scenicQueryVue
        },
        {
          path: 'diningQuery',
          name: 'diningQuery',
          component: diningQueryVue,
        },
        {
          path: 'showQuery',
          name: 'showQuery',
          component: showQueryVue,
        },
        {
          path: 'hotelOrder',
          name: 'hotelOrder',
          component: hotelOrderVue
        }
      ]
    },
    {
      path: '/manager',
      name: 'manager',
      component: mainManagerVue,
      redirect: '/manager/index',
      children: [
        {
          path: 'index',
          name: 'manager_index',
          component: () => import('@/views/managerView/index.vue'),
        },
        {
          path: 'user',
          name: 'manager_user',
          component: () => import('@/views/managerView/userView/userManagerView.vue'),
        },
        {
          path: 'complainhandle',
          name: 'manager_complainhandle',
          component: () => import('@/views/managerView/complainsView/complainhandle.vue'),
        },
        {
          path: 'finishcomplain',
          name: 'manager_finishcomplain',
          component: () => import('@/views/managerView/complainsView/finishcomplain.vue'),
        },
        {
          path: 'starhotel',
          name: 'manager_starhotel',
          component: () => import('@/views/managerView/hotelsView/starHotel.vue'),
        },
        {
          path: 'nonstarhotel',
          name: 'manager_nonstarhotel',
          component: () => import('@/views/managerView//hotelsView/nonStarHotel.vue'),
        },
        {
          path: 'warningrelease',
          name: 'manager_warningrelease',
          component: () => import('@/views/managerView/warningView/warningRelease.vue'),
        },
        {
          path: 'warningmanager',
          name: 'manager_warningmanager',
          component: () => import('@/views/managerView/warningView/warningManager.vue'),
        },
      ]
    },
    {
      path: '/hotelmanager',
      name: 'hotelmanager',
      component: mainHotelManagerVue,
      redirect: '/hotelmanager/index',
      children: [
        {
          path: 'index',
          name: 'index',
          component: () => import('@/views/hotelManagerView/hotelindex.vue'),
        },
        {
          path: 'swiper',
          name: 'swiper',
          component: () => import('@/views/hotelManagerView/RoomSetting.vue'),
        },
        {
          path: 'category',
          name: 'category',
          component: () => import('@/views/hotelManagerView/RoomSettingNoStar.vue'),
        }
      ]
    },
    {
      path: '/login',
      name: 'login',
      component: loginVue
    },
    {
      path: '/signUp',
      name: 'signUp',
      component: signUpVue
    },
    {
      path: '/resetPassword',
      name: 'resetPassword',
      component: resetPassword
    }
  ]
})


// 声明全局前置守卫，控制后台主页的访问权限
router.beforeEach((to, from, next) => {
  if (/^\/traveler\/|^\/manager\/|^\/hotelmanager\//.test(to.path)) {
    // 访问的是后台主页，需要判断是否登录
    // 获取本地的 token
    const token = localStorage.getItem('token')
    if (token) {
      // 已登录，直接放行
      next()
    } else {
      // 没登录，跳转到登录页
      ElMessage({
        message: 'token失效',
        type: 'warning',
        duration: 5 * 1000
      })
      next('/login')
    }
  } else {
    // 其他页面直接放行
    next()
  }
})

export default router
